<?php

namespace app\common\model;

use think\Model;
use utils\Tools\Tree;
use think\Cookie;

class Base extends Model {

    /**
     * @title 预处理
     */
    public function model_where($wheres = []) {

        foreach ($wheres as $key => $value) {
            if (isset($value[1]))
                $this->where($key, $value[0], $value[1]);
            else
                $this->where($key, $value[0]);
        }

        if (request()->get('keyword'))
            $this->where('title', 'like', '%' . request()->get('keyword') . '%');

        return $this;
    }

    /**
     * @title 用于树型select赋值
     */
    public function lists_select_tree($where = NULL, $order = 'listorder asc,id asc') {
        $result = $this->where($where)->order($order)->column('id,pid,title');
        $tree = new Tree($result);
        $lists = $tree->getArray(0);
        $lists2 = array();
        foreach ($lists as $key => $value) {
            $lists2[$value['id']] = $value['title'];
        }
        return $lists2;
    }

    /**
     * @title 将配置加载成树形结构
     * @param type $title
     */
    public function iframe_category_config_json($title) {
        foreach (config($title) as $key => $value) {
            $array_json[] = [
                'id' => $key, 'name' => $value
            ];
        }
        $parent = [
            'id' => 0,
            'name' => '所有分类',
            'spread' => true,
            'children' => $array_json
        ];
        return json_encode($parent);
    }

    /**
     * @title IFRAME分类
     */
    public function iframe_category_json() {

        $lists = $this->field('id,title')->order('id', 'asc')->select();

        foreach ($lists as $key => $value) {
            $array_json[] = [
                'id' => $value['id'], 'name' => $value['title']
            ];
        }
        $parent = [
            'id' => 0,
            'name' => '所有分类',
            'spread' => true,
            'children' => $array_json
        ];
        return json_encode($parent);
    }

    /**
     * @title IFRAME树型分类
     */
    public function iframe_category_tree_json() {
        $lists = $this->all(function($query) {
            $query->field('id,title,pid')->order('id', 'asc');
        });
        $lists = array_out($lists);
        $tree = new Tree($lists);
        $array = $tree->get_tree_array(0);
        $array_json = json_encode($array);
        $array_json = str_replace('title', 'name', $array_json);
        $parent = [
            'id' => 0,
            'name' => '所有分类',
            'spread' => true,
            'children' => json_decode($array_json, true)
        ];
        return json_encode($parent);
    }

    /**
     * @title 分类
     */
    public function json_category($name = 'name', $search = NULL) {
        $types = [];
        $lists = $this->where($search)->select();
        foreach ($lists as $key => $value) {
            $types[$key] = [
                'name' => $value[$name],
                'id' => $value['id']
            ];
        }
        $parent = [
            'id' => 0,
            'name' => '所有分类',
            'spread' => true,
            'children' => $types
        ];
        return json_encode($parent);
    }

}
